<?php
namespace Admin\Controller;
use Admin\Model\UserLogModel;

class SalesController extends MainController{
    /**
     * 获取在售商品列表
     * @queyuan
     * 2016/05/31
     * @param $flag：1-最新，2-人气，3-剩余，4-总需,$page,$c_id
     */
    public function sales(){
        $flag = I("param.flag");
        $page = I("param.page",0,'int');
        $c_id = I("param.c_id",0,'int');
        $where = "s.status = 1";
        $result = array();
        if($flag == 1){
        	$order = "s.create_time desc";
        }elseif($flag == 2){
        	$order = "s.sale_num desc";
        }elseif($flag == 3){
        	$order = "left_num asc";
        }elseif($flag == 4){
        	$order = "s.need_num desc";
        }else{
        	$order = "s.ord desc";
        }
        if($c_id){
        	$sons = M("category")->where("pid = ".$c_id." and status = 1")->count();
        	if($sons){
        		$where .= " and g.p_cat = ".$c_id;
        	}else{
        		$where .= " and g.sub_cat = ".$c_id;
        	}
        }
        $count = M("sales s")->join("left join one_goods g on (s.good_id = g.id)")->where($where)->count();
        $num = ceil($count / C("pagesize"));
        if($page < 0){
        	$page = 0;
        }
        if($page > $num){
        	$page = $num - 1;
        }
        $limit = C("pagesize") * $page;
        $list = M("sales s")->join("left join one_goods g on (s.good_id = g.id)")->where($where)->order($order)->field("s.id,s.good_id,s.price,s.need_num,s.sale_num,(s.need_num - s.sale_num) as left_num,g.name as good_name,g.index_img as img")->select();
        if($list){
        	$result['status'] = 0;
        	$result['msg'] = "请求成功";
        	$result['list'] = $list;
        	$result['page'] = $page;
        	$result['num'] = $num;
        }else{
        	$result['status'] = 1;
        	$result['msg'] = "无数据";
        }
        echo json_encode($result);
        exit;
    }

    /**
     * 获取商品详情
     * @queyuan
     * 2016/05/30
     * @param $id,$user_id
     */
    public function saleDetail(){
    	
    }
}
?>